package org.example.booksmanagementsystem.mapper;

import org.apache.ibatis.annotations.*;
import org.example.booksmanagementsystem.model.BookInfo;
import org.example.booksmanagementsystem.model.PageRequest;
import org.example.booksmanagementsystem.model.UserInfo;

import java.util.List;

@Mapper
public interface BookInfoMapper {


    //每页的数据
    @Select("select id,book_name,author,count,price,publish,status," +
            "create_time,update_time from book_info "+
            "where status !=3 and status !=0 order by id asc limit #{offset},#{pageSize} "
    )
    List<BookInfo> queryBookInfoByPage(PageRequest pageRequest);


    //数据的总条数
    @Select("select count(*) from book_info where status!=0;")
    Integer queryBookInfoCount();

    // 添加图书
    @Insert("Insert into book_info (book_name,author,count,price,publish,status) " +
            "values(#{bookName},#{author},#{count},#{price},#{publish},#{status})")
    Integer insertBookInfo(BookInfo bookInfo);

    // 根据图书Id查询信息
    @Select("select id,book_name,author,count,price,publish,status from book_info " +
            "where id =#{id} and status!=0")
    BookInfo queryBookInfoById(Integer id);

    //修改图书信息
    Integer updateBookInfo(BookInfo bookInfo);

    //批量删除图书
    Integer batchDeleteBookInfoById(@Param("idList") List<Integer> idList);

    //在用户图书表中查询图书
    @Select("select id,book_name,author,count,price,publish,status from #{userId}_book_info "+
            " where book_name = #{bookName}")
    BookInfo queryBookInfoByBookName(@Param("userId") Integer userId,@Param("bookName") String bookName);

    // 查询图书的 售卖库存
    @Select("select count from book_info where id = #{bookId} and status=1") //
    Integer queryBookCount(Integer bookId);

    // 买图书后更新图书 售卖库存 -1
    @Update("update book_info set count = count -1 where id = #{bookId}")
    Integer deleteBookCount(Integer bookId);

    // 取消买图书后更新图书 售卖库存 +1
    @Update("update book_info set count = count + 1 where id = #{bookId}")
    Integer addBookCount( Integer bookId);

    // 取消买图书后更新图书 售卖库存 +1
    @Update("update book_info set count = count + 1 where book_name = #{bookName} and author=#{author}")
    Integer addBookCountByBookinfo(BookInfo bookInfo);


    // 第一次购买图书，向用户图书表插入数据
    @Insert("Insert into #{userId}_book_info (book_name,author,count,price,publish,status) " +
            "values(#{bookInfo.bookName},#{bookInfo.author},1,#{bookInfo.price},#{bookInfo.publish},#{bookInfo.status})")
    Integer insertUserBookInfoTable(@Param("userId") Integer userId,@Param("bookInfo") BookInfo bookInfo);

    // 购买过图书，用户图书count+1
    @Update("update #{userId}_book_info set count=count+1 where book_name=#{bookName}")
    Integer addCountUserBookInfoTable(@Param("userId") Integer userId,@Param("bookName") String bookName);

    //取消购买图书，用户库存-1
    @Update("update #{userId}_book_info set count=count-1 where book_name=#{bookName}")
    Integer deleteUserBookInfoCount(@Param("userId") Integer userId,@Param("bookName") String bookName);

    // 用户图书的总数
    @Select("select count(*) from #{userId}_book_info")
    Integer queryBookInfoCountOfNormalUser(Integer userId);

    //获取用户购物车--分页
    @Select("select id,book_name,author,count,price,publish,status," +
            "create_time,update_time from #{userId}_book_info "+
            "where status !=0 and count >0 order by id asc "+
            " limit #{pageRequest.offset},#{pageRequest.pageSize} "
    )
    List<BookInfo> queryBookInfoOfNormalUserByPage(@Param("pageRequest") PageRequest pageRequest,@Param("userId") Integer userId);

    //从用户图书中查询图书信息
    @Select("select id,book_name,author,count,price,publish,status," +
            "create_time,update_time from #{userId}_book_info "+
            "where id = #{bookId} "
    )
    BookInfo queryBookInfoByNormalId(@Param("bookId") Integer bookId,@Param("userId") Integer userId);


    // 用户图书表的所有信息
    @Select("select id,book_name,author,count,price,publish,status, " +
            "create_time,update_time from #{userId}_book_info")
    List<BookInfo> batchNoBuyBook(Integer userId);

    // 使用图书名和作者在售卖图书查库存
    @Select("select id,book_name,author,count,price,publish,status," +
            "create_time,update_time from book_info "+
            "where  book_name= #{bookName} and author=#{author} and status=1"
    )
    BookInfo queryBookInfoByBookInfo(BookInfo bookInfo);
}
